// pages/detail/detail.js
const {getArticleDtail,getArticleCollect} = require("../../model/http")
const audio = wx.getBackgroundAudioManager();
Page({
  data: {
    lists:[],
    id:"",
    play:"",
    states:''
  },
  onLoad:async function(options) {
    var {id} = options;
    getArticleDtail(id).then(res=>{
      var lists = res.data.res[0];
      // console.log(id)
      let playUrl = lists.music.url;
      let titleUrl = lists.music.title
      let states = this.data.states
      if(states==true){
        audio.title = titleUrl;
        audio.src = playUrl;
        audio.play()
      }else if(states == false){
        audio.pause()
      }
      this.setData({lists,play:{playUrl,titleUrl,id},states})
    })
    wx.setNavigationBarTitle({
      title: '阅读',
    })
  },
  handleClick(){
    console.log("!")
    let states = this.data.states
    if(states==false){
      audio.pause()
    }else{
      audio.play()
    }
    this.setData({
      states:!states
    })
  },
  handleChange(){
    this.data.lists.collected = !this.data.lists.collected
    var lists = this.data.lists
    console.log(lists.collected)
    this.setData({
      lists
    })
    getArticleCollect({
      url:'api/doCollect',
      id:this.data.id,
      collected:this.data.lists.collected,
      success:res=>{
        // console.log(res)
      }
    })
  }
})